package com.zkk.web;

import com.sun.net.httpserver.HttpServer;
import com.zkk.pojo.Admin;
import com.zkk.service.AdminService;
import com.zkk.service.impl.AdminServiceImpl;
import sun.net.httpserver.HttpServerImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Admin admin=new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        AdminService adminService=new AdminServiceImpl();
        Admin login = adminService.login(admin);
        PrintWriter writer = resp.getWriter();
        if(login!=null){
            resp.sendRedirect("/findAll");
        }else {
            writer.println("登入失败");
        }
    }
}
